package com.yanqu.road.server.manger.cross3;

import com.yanqu.road.entity.enums.eChannelActivitySwitchType;
import com.yanqu.road.entity.player.PlayerState;
import com.yanqu.road.logic.bussiness.servercenter.ChannelActivityBussiness;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.pb.cross3.server.CrossChannelServerProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.ClientProtocol;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ChannelActivitySwitchMgr extends TempMgr {

    /**
     * 活动ID，开关类型，状态
     */
    private static Map<Integer, Map<Integer, Integer>> activitySwitchMap = new ConcurrentHashMap<>();

    /**
     * 刷新开关状态
     */
    public static void refreshSwitch() {
        try {
            // 先检查有没有数据库连接配置
            String dbParam = ChannelConfig.CHANNEL_CENTER_DB_PARAM;
            if ("0".equals(dbParam)) {
                getLogger().info("[ ChannelConfig.CHANNEL_CENTER_DB_PARAM ] is zero!");
                return;
            }

            // 读库
            activitySwitchMap = ChannelActivityBussiness.getActivitySwitchMap(dbParam);

            // 同步给在线玩家
            onlinePlayerSyncSwitch();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取状态
     *
     * @return 没配置默认0
     */
    public static int getStatus(int activityId, int switchType) {
        if (activitySwitchMap.containsKey(activityId) && activitySwitchMap.get(activityId).containsKey(switchType)) {
            return activitySwitchMap.get(activityId).get(switchType);
        }
        return 0;
    }

    /**
     * 跨地区活动聊天是否打开
     */
    public static boolean isChannelActivityChatOpen(int activityId) {
        int status = getStatus(activityId, eChannelActivitySwitchType.ChannelActivityChat.getValue());
        if (status == 0 || status == 1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 同步开关给在线玩家
     */
    public static void onlinePlayerSyncSwitch() {
        for (GamePlayer gamePlayer : GamePlayerMgr.getCopyPlayerMap().values()) {
            if (gamePlayer.getState() == PlayerState.ONLINE) {
                syncSwitch(gamePlayer);
            }
        }
    }

    /**
     * 同步开关
     */
    public static void syncSwitch(GamePlayer player) {
        CrossChannelServerProto.SyncChannelActivitySwitchRespMsg.Builder builder = CrossChannelServerProto.SyncChannelActivitySwitchRespMsg.newBuilder();
        for (Map.Entry<Integer, Map<Integer, Integer>> entry : new HashMap<>(activitySwitchMap).entrySet()) {
            int activityId = entry.getKey();
            for (Map.Entry<Integer, Integer> entry2 : entry.getValue().entrySet()) {
                int switchType = entry2.getKey();
                int status = entry2.getValue();
                builder.addSwitchList(
                        CrossChannelServerProto.ChannelActivitySwitchMsg.newBuilder().setActivityId(activityId).setSwitchType(switchType).setStatus(status)
                );
            }
        }
        player.sendPacket(ClientProtocol.U_SYNC_CHANNEL_ACTIVITY_SWITCH, builder);
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        refreshSwitch();
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }
}
